package com.lzy.cloud.payment.component.pay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * Description:支付机构管理类
 * 
 * @author lc Create Date Time:Mar 24, 2017 2:23:04 PM
 */
public class PaymentManager {
	private static final Logger LOGGER = LoggerFactory.getLogger(PaymentManager.class);

	private static Map<PayMethod, Class<? extends PayRequestHandler>> payServiceMap = new HashMap<PayMethod, Class<? extends PayRequestHandler>>();

	/**
	 * 将默认的方式初始化管理，并提供 addPayService()以方便运时添加
	 */
	static {
		// 支付宝
		addPayService(PayMethod.aliPay, null);
		// 微信
		addPayService(PayMethod.weixin, null);
		// 银行
		addPayService(PayMethod.bank, null);

	}

	/**
	 * 得到支付实例
	 * 
	 * @param payMethod
	 * @return
	 */
	public static PayRequestHandler getPayRequestHandler(PayMethod payMethod) {
		try {
			return payServiceMap.get(payMethod).newInstance();
		} catch (Exception e) {
			LOGGER.error("获取支付实例失败:" + payMethod, e);
		}
		return null;
	}

	/**
	 * 提供的对外接口,以方便运行时添加
	 * 
	 * @param payMethod
	 * @param payService
	 */
	public static void addPayService(PayMethod payMethod,
			Class<? extends PayRequestHandler> payService) {
		payServiceMap.put(payMethod, payService);
	}
}
